package com.xsf.jieyou.cloud.service;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.xsf.jieyou.cloud.api.CloudRedisSmsService;
import com.xsf.jieyou.cloud.api.CloudSmsService;
import com.xsf.jieyou.cloud.dto.SmsParam;
import com.xsf.jieyou.commons.utils.MapperUtils;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;

/**
 * 注册验证码发送接口实现类
 *
 * @author xsf
 * @version v1.0.0
 * @date 2019/11/19 16:47
 * @see com.xsf.jieyou.cloud.service
 **/
@Service(version = "1.0.0")
public class CloudSmsServiceImpl implements CloudSmsService {
    /**
     * 短信签名名称
     */
    private static final String SIGN_NAME = "解忧杂货店";

    @Reference(version = "1.0.0")
    private CloudRedisSmsService cloudRedisSmsService;

    /**
     * 注册账号短信验证发送
     * {
     * "Message":"OK",
     * "RequestId":"2184201F-BFB3-446B-B1F2-C746B7BF0657",
     * "BizId":"197703245997295588^0",
     * "Code":"OK"
     * }
     *
     * @param phoneNumber 手机号码
     * @return {@link SmsParam}
     */
    @Override
    public SmsParam sendRegisterSms(String phoneNumber) throws Exception {
        String templateCode = "SMS_175540285";
        // 随机验证码
        String code = RandomStringUtils.randomNumeric(4);
        // 将 验证码 存入Redis缓存中
        this.cloudRedisSmsService.insertRegisterCodeToRedis(phoneNumber, code);
        String templateParam = "{\"code\":\"" + code + "\"}";
        return MapperUtils.json2pojo(this.baseSendSms(phoneNumber, templateCode, templateParam), SmsParam.class);
    }

    /**
     * 发送登录短信验证码
     *
     * @param phoneNumber 手机号
     * @return {@link SmsParam}
     */
    @Override
    public SmsParam sendLoginSms(String phoneNumber) throws Exception {
        // 阿里云平台短信模板码
        String templateCode = "SMS_175530336";
        // 随机验证码
        String code = RandomStringUtils.randomNumeric(4);
        // 将 验证码 存入Redis缓存中
        this.cloudRedisSmsService.insertLoginCodeToRedis(phoneNumber, code);
        String templateParam = "{\"code\":\"" + code + "\"}";
        return MapperUtils.json2pojo(this.baseSendSms(phoneNumber, templateCode, templateParam), SmsParam.class);

    }

    /**
     * 发送短信服务方法
     *
     * @param phoneNumber   接收短信的手机号码
     * @param templateCode  短信模板ID
     * @param templateParam 短信模板变量对应的实际值，JSON格式
     * @return {"Message":"OK","RequestId":"2184201F-BFB3-446B-B1F2-C746B7BF0657","BizId":"197703245997295588^0",
     * "Code":"OK"}
     */
    private String baseSendSms(String phoneNumber, String templateCode, String templateParam) throws Exception {
        DefaultProfile profile = DefaultProfile.getProfile(
                "cn-hangzhou",
                "LTAI4FcQuPS9bQch4S5mTV6P",
                "Wmu7NhtskIDmeWr8wYVMgNsLOm26vX");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        // 接收短信的手机号码。
        request.putQueryParameter("PhoneNumbers", phoneNumber);
        // 短信签名名称。请在控制台签名管理页面签名名称一列查看。
        request.putQueryParameter("SignName", SIGN_NAME);
        // 短信模板ID。请在控制台模板管理页面模板CODE一列查看。示范：SMS_153055065
        request.putQueryParameter("TemplateCode", templateCode);
        // 短信模板变量对应的实际值，JSON格式。示范：{"code":"1111"}
        request.putQueryParameter("TemplateParam", templateParam);

        CommonResponse response = client.getCommonResponse(request);
        return response.getData();
    }
}
